package ControllerSrevlet;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import MYJDBC.jdbc;

/**
 * Servlet implementation class BlueVideoUpdate
 */
@WebServlet("/BlueVideoUpdate")
public class BlueVideoUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BlueVideoUpdate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String videoid = request.getParameter("videoid");
		request.setAttribute("videoid", videoid);
		jdbc jc = new jdbc();
		request.setAttribute("SqlUser",jc.getSqlUser());
		request.setAttribute("SqlPass",jc.getSqlPass());
		request.setAttribute("SqlName", jc.getSqlName());
		request.getRequestDispatcher("VideoUpdate.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String str[] = new String[9];
		
		String name = null;
		
		int i=0;
		if(ServletFileUpload.isMultipartContent(request)){
	            FileItemFactory factory = new DiskFileItemFactory();
	            ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
	            try {
	                List<FileItem> list = servletFileUpload.parseRequest(request);
	                for(FileItem item : list){
	                    if(item.isFormField()){
	                    	str[i]=item.getString("UTF-8");
	                    	i++;
	                    }else {
	                        str[i] = item.getName();
	                        if(item.getSize()!=0) {
	                        	item.write(new File("E:\\毕业设计\\zwmwork\\Music_web\\src\\main\\webapp\\Video\\"+item.getName()));
	                        }
	                        i++;
	                    }
	                }
	            } catch (FileUploadException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
		
		int id = Integer.parseInt(str[6]);
		
		jdbc jc = new jdbc();
		if(str[2]!=""&&str[3]!="") {
			try {
				jc.SqlUpdate("update video_table set videoname = '"+str[0]+"',videotext = '"+str[1]+"',videoimg = '"+str[2]+"',videosrc = '"+str[3]+"' where videoid = "+id+"");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if(str[2]!=""&&str[3]=="") {
			try {
				jc.SqlUpdate("update video_table set videoname = '"+str[0]+"',videotext = '"+str[1]+"',videoimg = '"+str[2]+"' where videoid = "+id+"");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (str[2]==""&&str[3]!="") {
			try {
				jc.SqlUpdate("update video_table set videoname = '"+str[0]+"',videotext = '"+str[1]+"',videosrc = '"+str[3]+"' where videoid = "+id+"");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else {
			try {
				jc.SqlUpdate("update video_table set videoname = '"+str[0]+"',videotext = '"+str[1]+"' where videoid = "+id+"");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		try {
			jc.UpdateClose();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		response.sendRedirect("BlueVideoUpdate?videoid="+str[6]+"");
		
	}

}
